package com.lkedu.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.lkedu.entity.UserBean;
import com.lkedu.utils.DataSourceUtils;

public class AdminDao {

	QueryRunner runner=new QueryRunner(DataSourceUtils.getDataSource());
	
	//查询所有用户
		public List<UserBean> getUsers(){
			String sql="select * from tb_user";
			List<UserBean> us=null;
			try {
				us=runner.query(sql, new BeanListHandler<UserBean>(UserBean.class));
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return us;
		}

		public int disState(String uid) {
			String sql="update tb_user set state='不可用' where uid=?";
			Object[] params= {uid};
			int result=-1;
			try {
				result=runner.update(sql,params);
				return result;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return result;
			
		}

		public int delUser(String uid) {
			String sql="delete from tb_user where uid=?";
			Object[] params= {uid};
			int result=-1;
			try {
				result=runner.update(sql,params);
				return result;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return result;
		}

		public int save(String uid, String username, String phone, String email, String sex) {
			String sql="update tb_user set username=?,phone=?,email=?,sex=? where uid=?";
			Object[] params= {username,phone,email,sex,uid};
			int result=-1;
			try {
				result=runner.update(sql,params);
				return result;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return result;
		}

}
